from rest_framework.viewsets import ModelViewSet
from utils.common_mixins import ResponseMixin
from .models import School, Task, AddrPoint
from .serializers import SchoolSerializer, TaskSerializer, AddrPointSerializer
from django_filters.rest_framework import DjangoFilterBackend
from rest_framework.filters import OrderingFilter
from utils.common_pagination import CommonPageNumberPagination
from rest_framework.response import Response


class SchoolView(ResponseMixin, ModelViewSet):
    queryset = School.objects.filter(is_delete=False, is_show=True)
    serializer_class = SchoolSerializer
    authentication_classes = []


class TaskView(ResponseMixin, ModelViewSet):
    queryset = Task.objects.filter(is_delete=False, is_show=True, task_status__in=[1, 2, 3])
    serializer_class = TaskSerializer

    filter_backends = [DjangoFilterBackend, OrderingFilter]
    filterset_fields = ['category', 'producer', 'consumer']
    ordering_fields = ['price', 'id']
    pagination_class = CommonPageNumberPagination

    def create(self, request, *args, **kwargs):
        print(request.data)
        return super().create(request)

    def get_queryset(self):
        query_set = super().get_queryset()
        return query_set.filter(school=self.request.user.school)


class AddrPointView(ResponseMixin, ModelViewSet):
    queryset = AddrPoint.objects.all()
    serializer_class = AddrPointSerializer

    # filter_backends = [DjangoFilterBackend]
    filterset_fields = ['type', 'school']
